package fms.core;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;

import fms.core.uplinkmsg.AcarsProtocol;
import fms.core.uplinkmsg.SendRules;
import fms.core.uplinkmsg.Sender;
import fms.core.uplinkmsg.UplinkMessage;
import fms.core.uplinkmsg.UplinkMessageData;
import fms.core.uplinkmsg.UplinkMsgHead;
import fms.core.uplinkmsg.UplinkMsgProtocol;
import fms.core.uplinkmsg.UplinkMsgProtocolBody;

public class FLPXmlBuilder {
    private  static Logger logger = Logger.getLogger(FLPXmlBuilder.class);

    public String build(String aContent, String upMsg, String replySMI) {
        logger.info("[xml构造] 开始进行上传xml构造...");
        DownLinkDatagramHeader header = null;
        try {
            header = DownLinkDatagramHeader.Parse(aContent);
        } catch (Exception e) {
            logger.error("[xml构造] 解析报文头出错!" + e.getMessage());
            return generateBlankUplinkMsg();
        }

        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
        UplinkMessage uplinkMessage = body.uplinkMessage;

        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
        uplinkMsgHead.setAppUpMsgType("UPLINKFMS");
        uplinkMsgHead.setAn(header.getAn());
        uplinkMsgHead.setFi(header.getFi().toString());
        uplinkMsgHead.setOriginAirport("");
        uplinkMsgHead.setDestAirport("");
        uplinkMsgHead.setSmi(replySMI);
        uplinkMsgHead.setAutoreply(true);
        uplinkMsgHead.setApptype("FMS");
        uplinkMsgHead.setAirline(header.getFi().toString().substring(0,2));
        uplinkMsgHead.setOnlyLastReply(true);
        uplinkMsgHead.setTag("");

        SendRules sendRules = new SendRules();
        List<Sender> senders = new ArrayList<Sender>();
        Sender sender = new Sender(SendRules.SenderType.Default, "", "", 0, 0);
        senders.add(sender);
        sendRules.setAllowMaxTime(600);
        sendRules.setSenders(senders);

        UplinkMessageData uplinkMessageData = new UplinkMessageData(upMsg);

        uplinkMessage.setHead(uplinkMsgHead);
        uplinkMessage.setData(uplinkMessageData);
        uplinkMessage.setSendRules(sendRules);
        uplinkMessage.setMsg("");
        try {
            logger.info("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化！");
            return AcarsProtocol.Serialize(uplinkMsgProtocol);
        } catch (Exception e) {
            logger.error("[xml构造] 序列化出错!" + e.getMessage());
        }
        return "";
    }
    
    

    public static  String build(String flightId,String aircraftId, String upMsg, String replySMI) {
        logger.info("[xml构造] 开始进行上传xml构造...");    
        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
        UplinkMessage uplinkMessage = body.uplinkMessage;

        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
        uplinkMsgHead.setAppUpMsgType("UPLINKFMS");
        uplinkMsgHead.setAn(aircraftId);
        uplinkMsgHead.setFi(flightId);
        uplinkMsgHead.setOriginAirport("");
        uplinkMsgHead.setDestAirport("");
        uplinkMsgHead.setSmi(replySMI);
        uplinkMsgHead.setAutoreply(true);
        uplinkMsgHead.setApptype("FMS");
        uplinkMsgHead.setAirline(flightId.substring(0,2));
        uplinkMsgHead.setOnlyLastReply(true);
        uplinkMsgHead.setTag("");

        SendRules sendRules = new SendRules();
        List<Sender> senders = new ArrayList<Sender>();
        Sender sender = new Sender(SendRules.SenderType.Default, "", "", 0, 0);
        senders.add(sender);
        sendRules.setAllowMaxTime(600);
        sendRules.setSenders(senders);

        UplinkMessageData uplinkMessageData = new UplinkMessageData(upMsg);

        uplinkMessage.setHead(uplinkMsgHead);
        uplinkMessage.setData(uplinkMessageData);
        uplinkMessage.setSendRules(sendRules);
        uplinkMessage.setMsg("");
        try {
            logger.info("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化！");
            return AcarsProtocol.Serialize(uplinkMsgProtocol);
        } catch (Exception e) {
            logger.error("[xml构造] 序列化出错!" + e.getMessage());
        }
        return "";
    }
    
    
    
    
    

    private String generateBlankUplinkMsg() {
        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
        UplinkMessage uplinkMessage = body.uplinkMessage;
        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
        uplinkMsgHead.setAppUpMsgType("UPLINKFMS");
        uplinkMsgHead.setAn("");
        uplinkMsgHead.setFi("");
        uplinkMsgHead.setOriginAirport("");
        uplinkMsgHead.setDestAirport("");
        uplinkMsgHead.setSmi("");
        uplinkMsgHead.setAutoreply(true);
        uplinkMsgHead.setApptype("FMS");
        uplinkMsgHead.setAirline("");
        uplinkMsgHead.setOnlyLastReply(true);

        SendRules sendRules = new SendRules();
        List<Sender> senders = new ArrayList<Sender>();
        Sender sender = new Sender(SendRules.SenderType.Default, "", "", 0, 0);
        senders.add(sender);
        sendRules.setAllowMaxTime(600);
        sendRules.setSenders(senders);

        UplinkMessageData uplinkMessageData = new UplinkMessageData("");
        uplinkMessageData.setText("");

        uplinkMessage.setHead(uplinkMsgHead);
        uplinkMessage.setData(uplinkMessageData);
        uplinkMessage.setSendRules(sendRules);
        try {
            logger.info("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化！");
            return AcarsProtocol.Serialize(uplinkMsgProtocol);
        } catch (Exception e) {
            logger.error("[xml构造] 序列化出错!" + e.getMessage());
        }
        return "";
    }
}
